package com.example.shopping_li.me;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.shopping_li.R;

import java.util.ArrayList;
import java.util.List;

public class loginActivity extends AppCompatActivity {
TextView loginTv,regTv;
ViewPager logVp;
List<Fragment> fragList;//viewpager的数据源
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        int pos = getIntent().getIntExtra("login", 0);//接收上一个页面的数据
        loginTv=findViewById(R.id.login_tv_log);
        regTv=findViewById(R.id.login_tv_reg);
        logVp=findViewById(R.id.login_vp);
        initPage();
        setVPListener();
        if (pos == 0) {
            loginTv.setTextColor(Color.rgb(200,10,200));
            regTv.setTextColor(Color.WHITE);
logVp.setCurrentItem(0);
        }else {
            regTv.setTextColor(Color.rgb(200,10,200));
            loginTv.setTextColor(Color.WHITE);
            logVp.setCurrentItem(1);
        }
    }

    private void setVPListener() {
        logVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @SuppressLint("ResourceAsColor")
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    loginTv.setTextColor(Color.rgb(200,10,200));
                    regTv.setTextColor(Color.WHITE);

                }else {
                    regTv.setTextColor(Color.rgb(200,10,200));;
                    loginTv.setTextColor(Color.WHITE);
                }
            }
        });
    }
    private void initPage() {
        fragList = new ArrayList<>();
        fragList.add(new LoginFragment());
        fragList.add(new RegFragment());
        LoginVpAdapter vpAdapter = new LoginVpAdapter(getSupportFragmentManager(), fragList);
        logVp.setAdapter(vpAdapter);

    }

    @SuppressLint("ResourceAsColor")
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_tv_log:
                loginTv.setTextColor(Color.rgb(200,10,200));
                regTv.setTextColor(Color.WHITE);
                logVp.setCurrentItem(0);
                break;
            case R.id.login_tv_reg:
                regTv .setTextColor(Color.rgb(200,10,200));;
                loginTv .setTextColor(Color.WHITE);
                logVp.setCurrentItem(1);
                break;
        }
    }
    public  void changePage(){
logVp.setCurrentItem(0);
    }
}
